package com.cognizant.ams.service;

import com.cognizant.ams.bean.*;
import com.cognizant.ams.dao.LdmenuMapper;
import com.cognizant.ams.dao.LdmenugrpMapper;
import com.cognizant.ams.dao.LdmenugrptomenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author likev
 * @version 1.0
 */
@Service
public class MenuRoleService {
    @Autowired
    private LdmenugrpMapper ldmenugrpMapper;
    @Autowired
    private LdmenugrptomenuMapper ldmenugrptomenuMapper;
    @Autowired
    private LdmenuMapper ldmenuMapper;

    public List<Ldmenugrp> getAllRole(){

        List<Ldmenugrp> ldmenugrpList=ldmenugrpMapper.selectByExample(new LdmenugrpExample());
        return  ldmenugrpList;
    }

    public List<Ldmenugrp> getOneRole(Ldmenugrp role){
        String menugrpcode=role.getMenugrpcode();
        String menugrpname=role.getMenugrpname();
        LdmenugrpExample example=new LdmenugrpExample();
        LdmenugrpExample.Criteria criteria=example.createCriteria();
        if (null!=menugrpcode&&!"".equals(menugrpcode)&&!"null".equals(menugrpcode)){
            criteria.andMenugrpcodeEqualTo(menugrpcode);
        }
        if (null!=menugrpname&&!"".equals(menugrpname)&&!"null".equals(menugrpcode)){
            criteria.andMenugrpnameEqualTo(menugrpname);
        }

        List<Ldmenugrp> ldmenugrpList=ldmenugrpMapper.selectByExample(example);
        return  ldmenugrpList;
    }

    /**
     * 添加菜单角色
     * @param role
     * @return 返回的行数
     */
    public int addRole(Ldmenugrp role){
        role.setMakedate(new Date());
        role.setMaketime("00:00:00");
        int result =ldmenugrpMapper.insert(role);
        return result;
    }

    public  int updateRole(Ldmenugrp ldmenugrp){
        LdmenugrpExample example=new LdmenugrpExample();
        LdmenugrpExample.Criteria criteria=example.createCriteria();
        int result=ldmenugrpMapper.updateByExample(ldmenugrp,example);
        return  result;
    }

    /**
     * 删除角色
     * @param menugrpcode
     * @return
     */
    public int deleteRole(String menugrpcode){
        LdmenugrpExample example=new LdmenugrpExample();
        LdmenugrpExample.Criteria criteria=example.createCriteria();
        criteria.andMenugrpcodeEqualTo(menugrpcode);
        int result=ldmenugrpMapper.deleteByExample(example);
        return result;
    }

    /**
     * 查询角色包含的菜单
     * @param rolecode
     * @return
     */
    public List<Ldmenu> getMenusbyRole(String rolecode){
        //根据角色编码查询到菜单编码
        LdmenugrptomenuExample example=new LdmenugrptomenuExample();
        LdmenugrptomenuExample.Criteria criteria=example.createCriteria();
        criteria.andMenugrpcodeEqualTo(rolecode);
        List<Ldmenugrptomenu> menunums= ldmenugrptomenuMapper.selectByExample(example);

        //组装IN参数（nodecodeList）
        List nodecodeList=new ArrayList();
        for (Ldmenugrptomenu roles:menunums
             ) {
           String nodecode=roles.getNodecode();
           nodecodeList.add(nodecode);
        }
        //根据菜单编码查询菜单详情
        List<Ldmenu> menus=new ArrayList<>();
        if (nodecodeList.size()>0){
            LdmenuExample example1=new LdmenuExample();
            LdmenuExample.Criteria criteria1=example1.createCriteria();
            criteria1.andNodecodeIn(nodecodeList);

            menus=ldmenuMapper.selectByExample(example1);
        }

        return  menus;
    }


    /**
     * 为角色分配菜单
     * @param ldmenugrptomenu
     * @return 返回的行数
     */
   public int addMenu2Role(Ldmenugrptomenu ldmenugrptomenu){
       return ldmenugrptomenuMapper.insert(ldmenugrptomenu);
   }

    /**
     * 为角色移除某菜单
     * @param ldmenugrptomenu
     * @return
     */
   public int removeMenuFromRole(Ldmenugrptomenu ldmenugrptomenu){
       LdmenugrptomenuExample example=new LdmenugrptomenuExample();
       LdmenugrptomenuExample.Criteria criteria=example.createCriteria();
       criteria.andMenugrpcodeEqualTo(ldmenugrptomenu.getMenugrpcode());
       criteria.andNodecodeEqualTo(ldmenugrptomenu.getNodecode());
       int reslut = ldmenugrptomenuMapper.deleteByExample(example);
       return  reslut;
   }
}
